home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3325 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  64 lines

  1. Path: castle.nando.net!news
  2. From: actuary@nando.net   (Bill McCarthy)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: command line argument help
  5. Date: 27 Jan 1996 21:15:32 GMT
  6. Organization: News & Observer Public Access
  7. Message-ID: <4ee4lk$fac@castle.nando.net>
  8. References: <4edfth$ok@muss.CIS.McMaster.CA>
  9. Reply-To: actuary@nando.net (Bill McCarthy)
  10. NNTP-Posting-Host: grail803.nando.net
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4edfth$ok@muss.CIS.McMaster.CA>, shadowfax writes:
  14. >to all the c gods out there:
  15. >
  16. >i am not having too much success with command line arguments.  i am 
  17. >trying to make a simple sumation executable for dos.  i am using borland 
  18. >c++ v3.1.  what i want as the end result is the user just types:
  19. >
  20. >c:\> sum 6 3
  21. >
  22. >and the executable would output an answer of 9.  the following is my program:
  23. >
  24. >
  25. >#include <stdio.h>
  26. >#include <stdlib.h>
  27. >
  28. >int main(char *argv[])
  29.  
  30. main takes no or two arguments.  Replace above with:
  31.  
  32. int main( int argc, char *argv[] )
  33.  
  34. >{
  35. >   int iSum = 0;
  36. >
  37. Now check that user typed both parameters:
  38.  
  39.     if ( argc != 3 )
  40.     {
  41.         fprintf(stderr,"Usage: sum a b; where a & b are ints.\n");
  42.         exit( EXIT_FAILURE );
  43.     }
  44.  
  45. >   iSum = atoi(argv[1]) + atoi(argv[2]);
  46. >   printf("\nthe answer is %d", iSum);
  47. >
  48. >   return(0);
  49. >}
  50. >
  51. >when i compile and run, it always outputs an anser of 0.  can anyone tell 
  52. >me what's wrong?
  53. >
  54. >sf
  55. >
  56. >
  57.  
  58.  
  59.  
  60. Bill McCarthy
  61. actuary@nando.net
  62. Wendell, NC  USA
  63.  
  64.